package com.example.mymarket.api;

import android.util.Log;

import com.example.mymarket.Data_model.CatalogData;
import com.example.mymarket.Data_model.CurrentData;
import com.example.mymarket.Data_model.HomeData;
import com.example.mymarket.Data_model.LoginData;
import com.example.mymarket.Data_model.ProductDetail;
import com.example.mymarket.Data_model.ResponseData;
import com.example.mymarket.Data_model.cartData;
import com.example.mymarket.Data_model.info;
import com.example.mymarket.interfaces.ResponseCallback;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

public class ApiService {
    private static final String baseURL = "http://hmapp.net:8888/";

    public static void GetHome(ResponseCallback<ResponseData<HomeData>> callback){
        OkGo.<String>get(baseURL + "wx/home/index")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResponseData<HomeData> result = new Gson().fromJson(response.body(),new TypeToken<ResponseData<HomeData>>(){}.getType());

                        callback.onSuccess(result);
                    }
                });
    }

    public static void GetCatalog(ResponseCallback<ResponseData<CatalogData>> callback){
        OkGo.<String>get(baseURL + "wx/catalog/index")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResponseData<CatalogData> result = new Gson().fromJson(response.body(),new TypeToken<ResponseData<CatalogData>>(){}.getType());
                        if (response.body() != null){
                            Log.e("TAG", "GetCatalog拿到数据了" );
                            Log.i("TAG", "result:" + result);
                        } else {
                            Log.e("TAG", "GetCatalog没有拿到数据!" );
                        }
                        callback.onSuccess(result);
                    }
                });
    }

    public static void login(String username, String password, ResponseCallback<ResponseData<LoginData>> callback) {
        OkGo.<String>post(baseURL + "wx/auth/login")
                .upJson("{\"username\": \"" + username + "\", \"password\": \"" + password + "\"}")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResponseData<LoginData> result = new Gson().fromJson(response.body(),new TypeToken<ResponseData<LoginData>>() {}.getType());
                        if (result != null) {
                            Log.e("TAG", "登录请求拿到数据了，结果：" + result.getErrmsg());
                        } else {
                            Log.e("TAG", "登录请求返回数据解析异常，或数据为空");
                        }
                        callback.onSuccess(result);
                    }
                });
    }

    public static void getProductDetail(int id,ResponseCallback<ResponseData<ProductDetail>> callback) {
        OkGo.<String>get(baseURL + "/wx/goods/detail?id=" + id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResponseData<ProductDetail> result = new Gson().fromJson(
                                response.body(),
                                new TypeToken<ResponseData<ProductDetail>>(){}.getType()
                        );
                        callback.onSuccess(result);
                    }
                });
    }

    public static void getCurrent(int id, ResponseCallback<ResponseData<CurrentData>>callback){
        OkGo.<String>get(baseURL + "wx/catalog/current?id=" + id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResponseData<CurrentData> result = new Gson().fromJson(
                                response.body(),
                                new TypeToken<ResponseData<CurrentData>>(){}.getType()
                        );
                        if (result != null){
                            callback.onSuccess(result);
                        } else {
                            callback.onSuccess(null);
                        }

                    }
                });
    }

    public static void getCart(String token ,ResponseCallback<ResponseData<cartData>> callback){
        OkGo.<String>get(baseURL + "wx/cart/index")
                .headers("x-litemall-token",token)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ResponseData<cartData> result = new Gson().fromJson(
                                response.body(),
                                new TypeToken<ResponseData<cartData>>(){}.getType()
                        );
                        Log.e("Cat", result.getErrmsg() );
                        callback.onSuccess(result);
                    }
                });
    }

}
